#include "TriangleApp.h"
#include "NeroGame\Nero.h"
#include "Win32\Win32.h"
#include "DirectX\DirectX11.h"

using namespace Game;
/*** Scene To Load ************************************************************/
//std::string GScene = "..\\..\\assets\\xml\\3D_Game_World.xml";
//TriangleApp TriangleApp::s_instance;

extern bool GIsRendering;

RTTI_DEFINITIONS(TriangleApp)

TriangleApp::TriangleApp(void)
	:NGame(),
	 mTriangle(NULL),
	 mColorShader(NULL),
	 mCurrentState(State::Color_State)
{
}

TriangleApp::~TriangleApp(void)
{
}

void TriangleApp::Initialize(const std::string&  scene)
{
	NGame::Initialize(scene);
	mColorShader = MY_NEW(Memory::HID_Rendering, "Color Shader") Graphics::ColorShader();
	mColorShader->Initialize();

	mTextureShader = MY_NEW(Memory::HID_Rendering, "Texture Shader") Graphics::TextureShader();
	mTextureShader->Initialize();

	mLightShader = MY_NEW(Memory::HID_Rendering, "Light Shader") Graphics::LightShader();
	mLightShader->Initialize();

	mLight = MY_NEW(Memory::HID_Rendering,"Light") Graphics::Light();
	mLight->Initialize();

	mTriangle = MY_NEW(Memory::HID_Rendering, "Triangle") Triangle();
	mTriangle->Initialize();

	mIsInitialized = true;
}

void TriangleApp::Deinitialize()
{
	if(mIsInitialized)
	{
		MY_DELETE(mTriangle);
		MY_DELETE(mLight);
		MY_DELETE(mLightShader);
		MY_DELETE(mTextureShader);
		MY_DELETE(mColorShader);
	}

	NGame::Deinitialize();
}

void TriangleApp::Update(float gameTime)
{
	NGame::Update(gameTime);
}

void TriangleApp::Render()
{
	static float rotation = 0.0f;
	if(mIsInitialized)
	{
		D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
		Graphics::D3DDevice* device = GetPlatform()->GetDevice()->As<Graphics::D3DDevice>();

		worldMatrix = device->GetWorldMatrix();
		projectionMatrix = device->GetProjectionMatrix();
		viewMatrix = Graphics::Utility::NeroToDXD(GetPlatform()->GetCamera()->GetViewMatrix());

		// Update the rotation variable each frame.
		rotation += (float)D3DX_PI * 0.01f;
		if(rotation > 360.0f)
		{
			rotation -= 360.0f;
		}

		if(mTriangle)
		{
			mTriangle->Render();
		}
		
		// Set the initial position of the camera.
		GetPlatform()->GetCamera()->SetPosition(NVector3(0.0f,0.0f,-10.0f));
		
		switch (mCurrentState)
		{
		case Color_State:	
			if(mColorShader)
			{
				mColorShader->Render(mTriangle->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
			}
			break;
		case Texture_State:	
			if(mTextureShader)
			{
				mTextureShader->Render(mTriangle->GetIndexCount(), mTriangle->GetInstanceCount(), worldMatrix, viewMatrix, projectionMatrix, Graphics::TextureResourceManager::GetInstance()->GetResource("SeaFloor"));
			}
			break;
		case Light_State:
			if(mLightShader)
			{
				D3DXMatrixRotationY(&worldMatrix, rotation);
				mLightShader->Render(mTriangle->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, GetPlatform()->GetCamera()->As<Graphics::Camera>(), mLight, Graphics::TextureResourceManager::GetInstance()->GetResource("SeaFloor"));
			}
		default:
			break;
		}
	}
}

void TriangleApp::EndGame()
{
	NGame::EndGame();
}